package com.glory.gloryquickui.view

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.adapter.RadioButtonSelectAdapter
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.entity.SingleChooseOptionsBean
import com.glory.gloryquickui.utils.FloatingWindowsUtils.PopTextAndPositionCallback
import com.glory.gloryquickui.viewmodel.QuickUIViewModel

/**
 *单选框
 */
class RadioButtonSelectController(context: Context?,  val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context) {
    private var tvRadioButtonSelectName: TextView? = null
    private var rvRadioButton: RecyclerView? = null
    private var mAdapter: RadioButtonSelectAdapter? = null
    private var optionList:ArrayList<SingleChooseOptionsBean.Option> = arrayListOf()
    override fun resLayoutId(): Int {
        return R.layout.layout_radio_button_select
    }

    override fun onBindView(data: FieldBean?) {
        tvRadioButtonSelectName!!.text = data?.config?.label
        val mLayoutManager = GridLayoutManager(context, 3)
        rvRadioButton!!.layoutManager = mLayoutManager
        setAdapter(data)
    }

    private fun setAdapter(data: FieldBean?) {
        if(data?.config?.option?.dataType == "manual") {
            optionList.clear()
            for (option in data?.config?.option!!.optionList) {
                if (data?.config?.option?.dependObject!!.isEmpty()) {
                    optionList.add(option)
                }
        }
            updateRadioButtonView()
        }else if (data?.config?.option?.dataType == "dictionary"){
            viewModel.searchDictionaryInfo(data)
        }else if (data?.config?.option?.dataType == "dataTable"){
            viewModel.searchDataTableInfo(data)
        }else if (data?.config?.option?.dataType == "third"){
            viewModel.searchDataThirdInfo(data)
        }
    }

    fun updateRadioButtonData(dataList:ArrayList<SingleChooseOptionsBean.Option>){
        this.optionList = dataList
        updateRadioButtonView()
    }

    fun updateRadioButtonView(){
        mAdapter = RadioButtonSelectAdapter(
            optionList,
            object : PopTextAndPositionCallback {
                override fun onSuccess(string: String?, potion: Int) {
                    val map = viewModel.pageData.value!!.toMutableMap()
                    map[data!!.vModel] =optionList[potion].value
                    viewModel.pageData.value = map
                }
            },
            data!!.disabled,data!!.config.border)
        rvRadioButton!!.adapter = mAdapter
    }

    override fun onCreatedView(view: View?) {
        tvRadioButtonSelectName = view?.findViewById(R.id.tvRadioButtonSelectName)
        rvRadioButton = view?.findViewById(R.id.rvRadioButton)
    }

    override fun masterPart(): View? {
        return rvRadioButton
    }
}